/**
 * Table每个Cell中选择框点击后所在行的数据
 * @param  {[type]} rowKey       所在行第一类的key
 * @param  {[type]} lockNum      可操作单元格的长度
 * @param  {[type]} orginRowData 点击单元格所在行的原始数据
 * @param  {[type]} keyVal       点击单元格所在类的key
 * @param  {[type]} cell         点击单元格的数据
 * @return {[type]}              点击单元格所在行的处理数据
 */
export const onTabCellCheckPress = function (rowKey, lockNumber, orginRowData, keyVal, cell) {
  let rowData = { ...orginRowData };
  const selctCheck = rowData[keyVal].isCheck;
  if (cell.x == 0) {
    Object.keys(rowData).forEach((rVal) => {
      const joc = rowData[rVal];
      rowData[rVal] = { ...joc, isCheck: !selctCheck };
    });
    const lockNum = Object.values(rowData).filter((kitem) => {
      return kitem.isInput && kitem.isLock;
    }).length;
    rowData[rowKey] = { ...rowData[rowKey], isLock: lockNum === lockNumber };
  } else {
    let joc = rowData[keyVal];
    rowData[keyVal] = { ...joc, isCheck: !selctCheck };
    const number = Object.values(rowData).filter((item) => {
      return item.hasE === false || (item.x !== 0 && item.isCheck);
    }).length;
    const lockNum = Object.values(rowData).filter((kitem) => {
      return kitem.isInput && kitem.isLock;
    }).length;
    const fistSel = number == Object.values(rowData).length - 1;
    rowData[rowKey] = { ...rowData[rowKey], isCheck: fistSel, isLock: lockNum === lockNumber };
  }
  return rowData;
};
/**
 * 判断是否全被选择
 * @param  {[type]} rowKey 所在行第一类的key
 * @param  {[type]} list   table表格数据
 * @return {[type]}        是否全被选中
 */
export const onTabCheckAll = function (rowKey, list) {
  const rowList = list.filter((item) => item[rowKey].isCheck);
  return list.length === rowList.length;
};
/**
 * [onTabLockPress description]
 * @param  {[type]} rowKey     所在行第一类的key
 * @param  {[type]} lockNumber 可操作单元格的长度
 * @param  {[type]} list       table表格数据
 * @param  {[type]} isLock     是锁定还是解锁判断
 * @return {[type]}            锁定单元格所在行的处理数据
 */
export const onTabLockPress = function (rowKey, lockNumber, list, isLock) {
  return list.map((item) => {
    var obj = {};
    Object.keys(item).forEach((keyVal) => {
      const joc = item[keyVal];
      var temp = {};
      if (joc.isCheck) {
        temp.isLock = isLock;
      }
      obj[keyVal] = { ...joc, ...temp };
    });
    const lockNum = Object.values(obj).filter((kitem) => {
      return kitem.isInput && kitem.isLock;
    }).length;
    obj[rowKey] = { ...obj[rowKey], isLock: lockNum === lockNumber };
    return obj;
  });
};

/**
 * 获取每行的电量总值
 * @param  {[type]} rowData 每一行的数据
 * @return {[type]}         [description]
 */
export const onTabElectricNumber = function (rowData) {
  return Object.values(rowData)
    .filter((item) => item.isInput)
    .reduce((pre, now) => (now.value !== '-' ? Number(now.value) : 0) + pre, 0)
    .toFixed(3);
};

/**
 * 获取每行的总收入，并转换单位为万元
 * @param  {[type]} rowData 每一行的数据
 * @return {[type]}         [description]
 */
export const onTabIncomeNumber = function (rowData) {
  if (rowData.electrovalency === undefined) {
    const income = Number(rowData.quantity != '-' ? rowData.quantity.value : 0);
    return (income / 10000).toFixed(4);
  }
  const income =
    Number(rowData.quantity != '-' ? rowData.quantity.value : 0) *
    Number(rowData.electrovalency.value !== '-' ? rowData.electrovalency.value : 0);
  return (income / 10000).toFixed(4);
};

export const onTabZHNumber = function (list) {
  let arr = [].concat(list);
  let last = arr.pop();
  Object.keys(last).forEach((keyVal) => {
    const item = last[keyVal];
    if (item.isInput) {
      last[keyVal].value = arr.reduce((pre, now) => Number(now[keyVal].value) + pre, 0).toFixed(3);
    }
  });
  last.offset.value = Number(last.quantity.value - onTabElectricNumber(last)).toFixed(3);
  return [].concat(arr, last);
};

export const onTabCheckJS = function (list) {
  let arr = [].concat(list);
  arr.pop();
  let jList = arr.filter((item) => item.contractName.contractId === undefined);
  var num = 0;
  jList.forEach((item) => {
    num += Object.values(item).filter((jitem) => {
      return jitem.isInput && Number(jitem.value) < 0;
    }).length;
  });
  return num !== 0;
};
